from typing import Optional
from sqlalchemy.orm import Session
from mooc.crud.crud_base import CRUDBase
from mooc.models.userapi import (
    UserapiCache,
    UserapiReply
)
from mooc.schemas.userapi import (
    UserapiCacheCreate, UserapiCacheUpdate,
    UserapiReplyCreate, UserapiReplyUpdate
)

class CRUDUserapiCache(CRUDBase[UserapiCache, UserapiCacheCreate, UserapiCacheUpdate]):
    def get_by_key(self, db: Session, *, key: str) -> Optional[UserapiCache]:
        return self.get_by_field(db, "key", key)

    def get_by_id(self, db: Session, *, id: int) -> Optional[UserapiCache]:
        return self.get_by_field(db, "id", id)

class CRUDUserapiReply(CRUDBase[UserapiReply, UserapiReplyCreate, UserapiReplyUpdate]):
    def get_by_rid(self, db: Session, *, rid: int) -> Optional[UserapiReply]:
        return self.get_by_field(db, "rid", rid)

    def get_by_id(self, db: Session, *, id: int) -> Optional[UserapiReply]:
        return self.get_by_field(db, "id", id)

# 创建实例
userapi_cache_crud = CRUDUserapiCache(UserapiCache)
userapi_reply_crud = CRUDUserapiReply(UserapiReply)